package com.lagou.stage04;

/**
 * 编写一个员工类   (1)属性: 姓名,年龄       如果年龄小于18龄大于150抛出年龄异常       如果姓名为null或者""抛出空异常
 * */
public class WorkerExceptionTest {
    private String name;
    private int age;

    public WorkerExceptionTest() {
    }

    public WorkerExceptionTest(String name, int age) throws NameException, AgeException {
        setName(name);
        setAge(age);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) throws NameException {
        if (null == name || "" == name) {
            throw new NameException("error name");
        } else {
            this.name = name;
        }
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) throws AgeException {
        if( 18 > age || 150 < age ){
            throw new AgeException("error age");
        } else {
            this.age = age;
        }
    }

    @Override
    public String toString() {
        return "WorkerExceptionTest{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public static void main(String[] args) {
        WorkerExceptionTest worker = null;
        try {
            //System.out.println("测试年龄异常");
            //worker = new WorkerExceptionTest("haha", 151);

            System.out.println("测试空姓名异常");
            worker = new WorkerExceptionTest("", 30);
        } catch (NameException e) {
            System.out.println(e);
        } catch (AgeException e) {
            System.out.println(e);
        }
        System.out.println(worker);

    }

}
